<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%
String ug = request.getHeader("User-Agent");
if(ug!=null){
    //if(ug.matches("(?i).*iphone.*")){
	if(false){
        //response.sendRedirect("http://download.hua1000.com/comingsoon.html");
        //response.sendRedirect("https://itunes.apple.com/us/app/hua-qian-shu/id941643948?l=zh&ls=1&mt=8");
    }else if(ug.matches("(?i).*android.*micromessenger.*")){
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
        <meta name="viewport" content="width=device-width, user-scalable=no , initial-scale=1.0">
        <style type="text/css">
        html,body,div,span,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,abbr,address,cite,code,del,dfn,em,img,ins,kbd,q,samp,small,strong,sub,sup,var,b,i,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary,time,mark,audio,video{ margin:0; padding:0; outline:0; font-size:100%; font: inherit; vertical-align:baseline;}
        article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section { display:block;}
        address, caption, cite, code, dfn,em, strong, i, th, var, optgroup { font-style: normal;  font-weight: normal;}
        h1, h2, h3, h4, h5, h6,abbr,acronym{ font-weight: normal;}
        input,button,textarea,select,optgroup,option{ font-family: inherit; font-size: inherit; font-style: inherit; font-weight: inherit;}
        input,button,textarea,select { font-size: 100%;}
        input[type="text"],input[type="password"],textarea{ outline-style:none; -webkit-appearance:none;}
        textarea{resize:none;}
        ol,ul{ list-style: none;}
        table{ border-collapse: collapse; border-spacing: 0;}
        caption,th{ text-align: left;}
        a,ins{ text-decoration: none;}
        a{ outline:none;}
        a:visited,a:active{ text-decoration:none;}

        img{border: none;width: auto;max-width: 100%;height: auto;}
        html {
            font-family: sans-serif;
            -webkit-text-size-adjust: 100%;
            -ms-text-size-adjust: 100%;
        }
        body{
            color: #fff;
            font: normal 15px/1.5 "Microsoft Yahei", "微软雅黑";
            background-color: #dedfe1;
            padding:0 30px;
        }
        .wrapper{
            margin: 0 auto;
            padding:20px 0 0;
            overflow: hidden;
            text-align: center;
        }
        .wrapper .content a{
            display:block;
            height:42px;
            line-height:42px;
            margin-top:60px;
            color: #fff;
            border-bottom:3px solid #06928a;
            border-radius: 2px;
            background-color: #1ba595;
        }
        </style>
    </head>
    <body>
        <div class="content"><font color=black>很抱歉，微信暂不支持安装包下载。<br>
        请点击右上角菜单并选择“在浏览器中打开”。<br>
        之后，下载过程将自动开始。</font></div>
    </body>
</html>
<%
   }else{
       String channel = request.getParameter("s");
       if (channel != null) {
            channel = channel.toUpperCase()+"_";
       }else{
            channel = "_";
       }

       java.io.File apkFolder = new java.io.File("/data/images/apk");
       long maxLastModifiedDefault = 0;
       String latestApkDefault = null;
       long maxLastModified = 0;
       String latestApk = null;
       if (apkFolder.exists() && apkFolder.isDirectory()) {
           for (String f : apkFolder.list()) {
               if (f.endsWith(".apk")) {
                   long lastModified = new java.io.File(apkFolder, f).lastModified();
                   if (f.toUpperCase().startsWith(channel) && lastModified > maxLastModified) {
                       maxLastModified = lastModified;
                       latestApk = f;
                   }
                   if (f.toUpperCase().startsWith("DEFAULT_") && lastModified > maxLastModifiedDefault) {
                       maxLastModifiedDefault = lastModified;
                       latestApkDefault = f;
                   }
               }
           }
           
       }
       if (latestApk == null) {
           latestApk = latestApkDefault;
       }
       if (latestApk == null) {
           latestApk = "unknown.html";
       }
       response.sendRedirect("http://image.petany.com/apk/"+latestApk);
   }
}
%>
